package com.cc.jdkjucdemo.CompletableFuture;


import java.util.concurrent.*;

public class CompletableFutureDemo03 {

    static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,
            50,
            10,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(100),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 没有返回结果，可以执行任务
//        CompletableFuture.runAsync(() -> {
//            System.out.println("子线程执行了：" + Thread.currentThread().getName());
//            int i = 1 / 0;
//        }, executor).handleAsync((result, exec) -> {
//            System.out.println("exec = " + exec);
//            return null;
//        }).get();

        // 带返回结果
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("子线程2执行了：" + Thread.currentThread().getName());
//            int i = 1 / 0;
            return 10;
        }, executor).handleAsync((result, exec) -> {
            if (exec != null) {
                System.out.println("exec = " + exec);
                return 55;
            } else {
                return result;
            }
        });
        Integer integer = integerCompletableFuture.get();
        System.out.println(integer);

        executor.shutdown();
    }
}
